<template>
	<view class="xc-wrap">

		<view class="xc-block">

			<uv-form labelPosition="left" :model="control.form" :rules="control.rules" ref="form">
				<uv-form-item label="地址" prop="title" required borderBottom>
					<cityPicker :column="column" :default-value="defaultValue" :mask-close-able="maskCloseAble"
						@confirm="confirm" @cancel="cancel" :visible="visible" />

					<uv-input v-model="control.form.area" @tap="open" border="surround">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="站点名称" prop="name" required borderBottom>
					<uv-input v-model="control.form.name" border="surround">
					</uv-input>
				</uv-form-item>

				<uv-form-item label="资产码" prop="memo" required borderBottom>
					<uv-input v-model="control.form.code" border="surround">
					</uv-input>
				</uv-form-item>

				<uv-form-item label="现场情况" prop="memo" required borderBottom>
					<uv-picker ref="picker" :columns="columns" @confirm="confirm3"></uv-picker>
					<uv-input v-model="control.form.qingkuang" @tap="openPicker" border="surround" disabled="true">
					</uv-input>
				</uv-form-item>

				<uv-form-item label="事故原因" prop="memo" required borderBottom>
					<uv-picker ref="picker2" :columns="columns2" @confirm="confirm2"></uv-picker>

					<uv-input v-model="control.form.yuanyin" @tap="openPicker2" border="surround" disabled="true">
					</uv-input>
				</uv-form-item>


				<uv-form-item label="事故描述" prop="memo" required borderBottom>
					<uv-textarea v-model="control.form.miaoshu" border="surround">
					</uv-textarea>
				</uv-form-item>

				<uv-form-item label="图片" prop="image" required borderBottom>
					<xc-uv-upload v-model="control.form.image" maxCount="1"></xc-uv-upload>
				</uv-form-item>


			</uv-form>
			<uv-button type="primary" text="提交" customStyle="margin-top: 10px" @tap="control_submit"></uv-button>

		</view>
	</view>
</template>

<script>
	import cityPicker from '@/uni_modules/piaoyi-cityPicker/components/piaoyi-cityPicker/piaoyi-cityPicker';
	import config from "@/config/config.js";
	import xcUniappUtils from '@/library/xcore/js/XcUniappUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	import xcCloginUtils from '@/library/xcore/js/XcCloginUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';

	export default {
		data() {
			return {
				visible: false,
				columns: [
					['一级', '二级', '三级']
				],
				columns2: [
					['车辆原因', '设备原因']
				],
				maskCloseAble: true,
				str: '',
				defaultValue: '420103',
				// defaultValue: ['河北省','唐山市','丰南区'],
				column: 3,
				str2: '',
				radioList: [{
						text: '安全员',
						value: 1,
						checked: true
					},
					{
						text: '项目经理',
						value: 2,
						checked: false
					}
				],
				selectedValue: 1, // 用于存储选中的值
				shenfen: '无',
				config: config,
				xsStringUtils: xsStringUtils,
				xsDateUtils: xsDateUtils,
				sp: {},
				sr: {},
				control: {
					form: {
						image: '',
						type: 1
					}
				}
			}
		},
		components: {
			cityPicker
		},
		onLoad(options) {
			let that = this;
			that.xinit();
		},
		onShow() {
			let that = this;
			xcUniappUtils.onShow(function() {
				that.public_show();
			});
		},
		onPullDownRefresh() {
			let that = this;
			setTimeout(function() {
				that.public_show();
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			openPicker() {
				this.$refs.picker.open();
			},
			openPicker2() {
				this.$refs.picker2.open();
			},
			confirm2(val) {
				this.control.form.yuanyin = val.value[0];
			},
			confirm3(val) {
				this.control.form.qingkuang = val.value[0];

			},
			open() {
				this.visible = true
			},
			confirm(val) {
				console.log(4444, val)
				this.str2 = ''
				// this.str = JSON.stringify(val)
				this.str = val
				console.log(5555, this.str)
				this.control.form.area = this.str.name
				console.log(66666, this.str.name)
				this.visible = false
			},

			cancel2() {
				this.visible = false
			},

			handleRadioChange(e) {
				this.control.form.type = e.detail.value;
			},
			xinit() {
				let that = this;
			},
			public_reset() {
				let that = this;
				that.public_show();
			},
			public_show() {
				let that = this;

				xcCloginUtils.getUserinfo(function(userinfo) {

					that.shenfen = '无'

					if (userinfo.shenfen == 1) {
						that.shenfen = '安全员'
					}

					if (userinfo.shenfen == 2) {
						that.shenfen = '项目经理'
					}

					xcHttpUtils.row2form(userinfo, that.control.form);
				});



			},
			control_submit() {
				let that = this;
				let control = that.control;
				console.log(3333, control.form)
				if (!that.control.form.name || !that.control.form.code) {
					uni.showToast({
						title: '请填写完整信息',
						icon: "none",
						duration: 2000
					});

					return
				}

				xcHttpUtils.yujingtijiao(control.form, function(res2) {
					uni.showToast({
						title: res2,
						icon: "none",
						duration: 2000
					});
				});

			},
			control_clickDelete() {
				let that = this;
				xcViewUtils.router_gotoPage("/pages/xclogin/me_delete")
			},
			mp_control_changeNick(e) {
				let that = this;
				let control = that.control;
				control.form.nickname = e.detail.value;
			},
			mp_control_onChooseAvatar(e) {
				let that = this;
				let control = that.control;
				xcHttpUtils.upload(e.detail.avatarUrl, function(url) {
					control.form.avatar = url;
				});
			},
		},

	}
</script>

<style lang="scss">
	.xp-mp-avatar {
		position: absolute;

		.avatar-wrapper {
			padding: 0px;
		}
	}

	.container {
		padding: 20px;
	}

	.radio-item {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
	}
</style>